package com.hdu.lisener.core;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * 监听器适配器
 *
 * @param <T>
 */
public class ApplicationListenerAdapter<T extends ApplicationEvent> implements ApplicationListener<T> {

    private final Object bean;
    private final Method method;
    private final Class<T> eventType;

    public ApplicationListenerAdapter(Object bean, Method method, Class<T> eventType) {
        this.bean = bean;
        this.method = method;
        this.eventType = eventType;
        this.method.setAccessible(true);
    }

    @Override
    public void onApplicationEvent(T event) {
        try {
            method.invoke(bean, event);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
